package com.ruoyi.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.business.service.SqlServerService;
import com.ruoyi.business.vo.SqlServerUserVo;
import com.ruoyi.common.constant.RoleConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.service.ISysUserRoleService;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Sql Server 人员库
 *
 * @author Tellsea
 * @date 2024−04−25
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SqlServerServiceImpl implements SqlServerService {

    private final ISysUserService sysUserService;
    private final ISysUserRoleService sysUserRoleService;

    @Override
    public void exportUser() {
        String response = HttpUtil.get("https://ydyxkfyy.huashan.org.cn:5932/jeecg-boot/outApi/queryXxqgView");
        if (StrUtil.isEmpty(response)) {
            log.info("exportUser 数据为空 !");
            throw new ServiceException("数据为空");
        }
        if (!JSONUtil.isJson(response)) {
            log.info("exportUser 数据格式异常 !");
            throw new ServiceException("数据格式异常");
        }
        JSONObject res = JSONUtil.parseObj(response);
        List<SqlServerUserVo> list = new ArrayList<>();
        JSONArray result = JSONUtil.parseArray(res.get("result"));
        result.forEach(e -> {
            JSONObject json = (JSONObject) e;
            SqlServerUserVo vo = new SqlServerUserVo();
            vo.setUserName(json.getStr("工号"));
            vo.setNickName(json.getStr("姓名"));
            vo.setSex(json.getStr("性别"));
            vo.setPhoneNumber(json.getStr("手机号"));
            vo.setDeptName(json.getStr("科室"));
            vo.setPostName(json.getStr("聘任专技职务"));
            vo.setSource(json.getStr("人员库"));
            vo.setTimestamp(json.getLong("时间戳"));
            list.add(vo);
        });
        if (CollUtil.isEmpty(list)) {
            log.info("exportUser 解析数据为空 !");
            return;
        }
        int index = 1;
        for (SqlServerUserVo userVo : list) {
            index++;
            log.info("{} userVo: {}", index, userVo);
            if (StrUtil.isEmpty(userVo.getUserName())) {
                continue;
            }
            SysUser sysUser = sysUserService.lambdaQuery().eq(SysUser::getUserName, userVo.getUserName()).one();
            if (ObjectUtil.isEmpty(sysUser)) {
                sysUser = new SysUser();
                sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
                sysUser.setUserName(userVo.getUserName());
                sysUser.setNickName(userVo.getNickName());
                sysUser.setPhonenumber(userVo.getPhoneNumber());
                sysUser.setSex(StrUtil.equals(userVo.getSex(), "男") ? "0" : "1");
                sysUser.setStatus(StrUtil.equals(userVo.getSource(), "离退人员库") ? "1" : "0");
                sysUserService.save(sysUser);
                sysUserRoleService.save(new SysUserRole().setUserId(sysUser.getUserId()).setRoleId(RoleConstants.TEACHER.getRoleId()));
            } else {
                sysUser.setNickName(userVo.getNickName());
                sysUser.setPhonenumber(userVo.getPhoneNumber());
                sysUser.setSex(StrUtil.equals(userVo.getSex(), "男") ? "0" : "1");
                sysUser.setStatus(StrUtil.equals(userVo.getSource(), "离退人员库") ? "1" : "0");
                sysUserService.updateById(sysUser);
            }
        }
    }
}
